package com.cm386.final_project_timezonealert;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.cm386.final_project_timezonealert.dao.DBService;
import com.cm386.final_project_timezonealert.model.City;
import com.cm386.final_project_timezonealert.view.CityListItems;

public class CityActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_city);
		ActionBar actionBar = this.getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		final ArrayList<City> items = (ArrayList<City>) this.getIntent().getSerializableExtra("CityList");
		List<CityListItems> cityListItems = new ArrayList<CityListItems>(); 
		for (City city : items)
			cityListItems.add(new CityListItems(city.getName()));
		
		int[] to      = new int[] {R.id.cityTextView};
		String[] from = new String[] {CityListItems.CITY_NAME_KEY};
		SimpleAdapter adapter = new SimpleAdapter(this, cityListItems, R.layout.city_item, from, to);
		
		ListView cityListView = (ListView) this.findViewById(R.id.citylistView);
		cityListView.setAdapter(adapter);
		
		cityListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				City city = items.get(position);
				DBService.getInstance(CityActivity.this).insertSelectedCity(city);
				Intent mainActivity = new Intent(CityActivity.this, MainActivity.class);
				mainActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mainActivity);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.city, menu);
		return true;
	}

}
